package com.gyms.activity;

import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.classic.okhttp.services.IUserService;
import com.classic.okhttp.zmoumall.http.ObjectCallback;
import com.classic.okhttp.zmoumall.utils.MD5;
import com.classic.okhttp.zmoumall.utils.ToastUtil;
import com.gyms.R;
import com.gyms.base.MyAutoLayoutActivity;
import com.gyms.manager.TitleManager;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import butterknife.BindView;
import butterknife.OnClick;
import constant.EventConstant;
import utils.StringUtils;
import weight.UiEvent;

/**
 * 修改密码页面 yjl
 */
public class EditPassActivity extends MyAutoLayoutActivity {
    @BindView(R.id.et_password)
    EditText etPassword;
    @BindView(R.id.et_newpass)
    EditText etNewpass;
    @BindView(R.id.et_newpass_again)
    EditText etNewpassAgain;
    @BindView(R.id.tv_find_pass)
    TextView tvFindPass;

    String oldPass,passWord,conFimPass;
    @Override
    protected void setTitleTheme() {
        mTheme = TitleManager.TitleTheme.White;
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_edit_pass;
    }

    @Override
    protected void initView() {
        setTitle("修改密码");
        createLoadingView("请求中",true);
        setBack();
        mTitleManager.setRightText("确定");
        EventBus.getDefault().register(this);
    }

    @Override
    protected void setListener() {
        mTitleManager.getmRightTV().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                oldPass=etPassword.getText().toString().trim();
                passWord=etNewpass.getText().toString().trim();
                conFimPass=etNewpassAgain.getText().toString().trim();
                if(StringUtils.isEmpty(oldPass)||StringUtils.isEmpty(passWord)||StringUtils.isEmpty(conFimPass)){
                    ToastUtil.showToast(mContext,"请输入密码");
                    return;
                }
                if(!passWord.equals(conFimPass)){
                    ToastUtil.showToast(mContext,"两次密码不一致");
                    return;
                }
                String oldpass= MD5.MD5Encode(new StringBuffer(oldPass).toString());
                String pass= MD5.MD5Encode(new StringBuffer(conFimPass).toString());
                req(oldpass,pass);
            }
        });
    }

    private void req(String oldPass,String pass){
        loadingView.show();
        IUserService.changePassword(this, oldPass, pass, false, new ObjectCallback<Object>() {
            @Override
            public void onSuccess(Object response, String message) {
                dialogDis();
                ToastUtil.showToast(mContext,"修改密码成功");
                finish();
            }

            @Override
            public void onErrorT(int code, Object response, String errorMessage) {
                dialogDis();
                ToastUtil.showToast(mContext,errorMessage);
            }

            @Override
            public void onHttpErrorCall(int statusCode) {
                dialogDis();
            }
        });
    }

    @Override
    protected void processLogic() {

    }


    @OnClick(R.id.tv_find_pass)
    public void onClick() {
        startActivity(FindPassActivity.class, false);
    }
    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        hideKeyBoard(ev);//隐藏软键盘
        return super.dispatchTouchEvent(ev);
    }
    @Subscribe
    public void doFindPassSuc(UiEvent mUiEvent){
        if(mUiEvent.getKey().equals(EventConstant.FLAG_FIND_PASS_SUC)){
            finish();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
